package com.jake.springboot.jwt.controller;

import com.jake.springboot.jwt.config.JwtConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class TokenController {

    @Autowired
    private JwtConfig jwtConfig;

    /**
     * 拦截器直接放行，返回Token
     */
    @PostMapping("/login")
    public Map<String, String> login(@RequestParam("username") String username,
                                     @RequestParam("password") String password) {
        Map<String, String> result = new HashMap<>();
        // 省略数据源校验
        String token = jwtConfig.getToken(username + password);
        if (!StringUtils.isEmpty(token)) {
            result.put("token", token);
        }
        result.put("username", username);
        return result;
    }

    /**
     * 需要 Token 验证的接口
     */
    @GetMapping("/info")
    public String info() {
        return "info";
    }

}
